import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceList: [],
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    storeItem: {},
    supplyItem: {}, //供应商
    orgType: wx.getStorageSync('userInfo').orgType
  },


  onLoad: function (options) {
    this.getStoreList();
    this.getSupplierList()
  },

  // 获取列表
  getSupplierList: function () {
    var that = this
    let url = '/org/protected/bizOrg/findAgentProxySupList'
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: {}
    }, (res) => {
      var data = res.data
      console.log(res);
      if (data && data.length == 1) {
        that.setData({
          supplyItem: {
            id: data[0].id,
            supName: data[0].supName
          }
        })
      }
      console.log(this.data.supplyItem);
    })
  },

  // 获取列表
  getStoreList: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/bizOrg/findStoreList",
      method: "post",
      data: {
        keyword: that.data.keywords,
        status: 1,
        pageSize: 10000
      }
    }, function (res) {
      console.log(res, 111111);
      if (res.list && res.list.length > 0) {
        that.setData({
          storeItem: res.list[0]
        })
      }

    })
  },

  onShow: function () {
    let cartList = wx.getStorageSync('goodsOdrList')
    this.setData({
      cartList
    })
  },

  clearOrder () {
    wx.setStorageSync('goodsOdrList', [])
    this.setData({
      cartList: []
    })
  },

  handleChoose: function (e) {
    var params = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/publish/template/commonlist?apiurl=' + params.apiurl + '&name=' + params.name + '&itemName=' + params.itemname + '&addUrl=' + params.addurl
    })
  },

  handleStore: function () {
    wx.navigateTo({
      url: '/pages/publish/template/store',
    })
  },

  toShop: function() {
    if (this.data.supplyItem.id || this.data.supplyItem.id != undefined) {
      wx.navigateTo({
        url: '/pages/publish/offline_order/shop?supId='+this.data.supplyItem.id,
      })
    } else {
      wx.showToast({
        title: '请先选择供应商',
      })
      return
    }
  },

  //表单提交
  formSubmit: function (e) {
    var formdata = e.detail.value;

    formdata.supId = this.data.supplyItem.id;
    // formdata.supName = this.data.supplyItem.supName;
    formdata.shoppingCartList = this.data.cartList;
    console.log(this.data.storeItem);

    // return

    if (this.data.cartList.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请选择商品',
      })
      return
    }

    if (formdata.supName == '') {
      wx.showToast({
        icon: 'none',
        title: '供应商名称不能为空',
      })
      return
    }

    if (!this.data.storeItem.stoName) {
      wx.showToast({
        icon: 'none',
        title: '请选择店铺',
      })
      return
    }

    // formdata.stoId = this.data.storeItem.id
    formdata.cusId = this.data.storeItem.userId
    //formdata.typeId=this.data.deviceList[this.data.typeIndex].id

    console.log(formdata, 'formdata')
    this.formRequest(formdata);
  },




  //请求
  formRequest: function (formdata) {
    var url = '/odr/protected/order/submit2BOfflineOrder';
    var title = "下单成功"

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      wx.setStorageSync('goodsOdrList', [])
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/data/index',
        })
      }, 1500)

    })
  },

})